// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: discovery support client and server
// ------------------------------------------------------------------

#ifndef _RTPS_FLOWCONTROL_FLOWCONTROLLERFACTORY_HPP_
#define _RTPS_FLOWCONTROL_FLOWCONTROLLERFACTORY_HPP_

#include <edds/rtps/attributes/WriterAttributes.h>

#include <string>
#include <map>

#include <edds/rtps/flowcontrol/FlowControllerDescriptor.hpp>

#include "FlowController.hpp"

namespace evbs {

namespace ertps {
namespace rtps {
class RTPSParticipantImpl;
}  // namespace rtps
}  // namespace ertps

namespace edds {
namespace rtps {

class FlowController;

const char* const pure_sync_flow_controller_name = "PureSyncFlowController";
const char* const sync_flow_controller_name = "SyncFlowController";
const char* const async_flow_controller_name = "AsyncFlowController";
/*!
 * Factory of flow controllers.
 *
 * @note Non-safe thread
 */
class FlowControllerFactory {
 public:
    /*!
     * Initialize the factory.
     * In charge of creating default flow controllers.
     * Call always before use it.
     *
     * @param participant Pointer to the participant owner of this object.
     */
    void init(ertps::rtps::RTPSParticipantImpl* participant);

    /*!
     * Registers a new flow controller.
     * This function should be used by the participant.
     *
     * @param flow_controller_descr FlowController descriptor.
     */
    void register_flow_controller(const FlowControllerDescriptor& flow_controller_descr);

    /*!
     * Get a FlowController given its name.
     *
     * @param flow_controller_name Name of the interested FlowController.
     * @return Pointer to the FlowController. nullptr if no registered FlowController with that
     * name.
     */
    FlowController* retrieve_flow_controller(const std::string& flow_controller_name,
                                             const ertps::rtps::WriterAttributes& writer_attributes);

 private:
    ertps::rtps::RTPSParticipantImpl* participant_ = nullptr;

    //! Stores the created flow controllers.
    std::map<std::string, std::unique_ptr<FlowController>> flow_controllers_;
};

}  // namespace rtps
}  // namespace edds
}  // namespace evbs

#endif  // _RTPS_FLOWCONTROL_FLOWCONTROLLERFACTORY_HPP_
